package com.gxa.saas.controller;

import com.gxa.saas.pojo.Purchase;
import com.gxa.saas.pojo.PurchaseRequest;
import com.gxa.saas.pojo.Storage;
import com.gxa.saas.service.PurchaseRequestService;
import com.gxa.saas.service.StorageService;
import com.gxa.saas.utils.JsonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/PurchaseRequest")
public class PurchaseRequestController {

    @Autowired
    PurchaseRequestService purchaseRequestService;



    @ApiOperation("添加操作")
    @RequestMapping(value = "/insert",method = {RequestMethod.POST})
    @ResponseBody
    public Map insert(@RequestBody() PurchaseRequest purchaseRequest){
        JsonResult jsonResult = new JsonResult();
        Integer insert = purchaseRequestService.insert(purchaseRequest);
            jsonResult.setCode("200");
            jsonResult.setMsg("添加成功");
            jsonResult.setData(insert);
        return jsonResult.getMap();
    }

    @ApiOperation("第二页分页查询")
    @RequestMapping(value = "/selectBySecondPage",method = {RequestMethod.POST})

    public Map selectByPage(@RequestParam(value = "page") Integer offset,
                            @RequestParam(value = "size",required = false,defaultValue = "10")Integer size){
        List<PurchaseRequest> purchaseRequests = purchaseRequestService.selectBySecondPage(offset, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(purchaseRequests);
        return jsonResult.getMap();
    }

    @ApiOperation("根据商品名称模糊分页查询")
    @RequestMapping(value = "/selectLikeName",method = {RequestMethod.POST})
    public Map selectLikeName(@RequestParam(value = "name") String name,
                              @RequestParam(value = "page") Integer offset,
                              @RequestParam(value = "size",required = false,defaultValue = "10") Integer size){
        List<PurchaseRequest> purchaseRequests = purchaseRequestService.selectLikeName(name, size * (offset - 1), size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(purchaseRequests);
        return jsonResult.getMap();
    }

    @ApiOperation("修改")
    @RequestMapping(value = "/update",method = {RequestMethod.POST})
    @ResponseBody
    public Map update(@RequestBody() Purchase purchase){
        JsonResult jsonResult = new JsonResult();
        Integer update = purchaseRequestService.update(purchase);
        jsonResult.setCode("200");
        jsonResult.setData(update);
        return jsonResult.getMap();
    }
}
